package cn.rmc.org.controller;

import cn.rmc.basic.util.AjaxResult;
import cn.rmc.basic.util.Pagelist;
import cn.rmc.org.domain.Shop;
import cn.rmc.org.domain.ShopAudit;
import cn.rmc.org.query.ShopQuery;
import cn.rmc.org.service.IShopService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(value = "/shop",description = "宠物之家组织结构模块店铺管理的接口规范！")
public class ShopController {
    @Autowired
    private IShopService shopService;

    @GetMapping("/{id}")
    public Shop getById(@PathVariable("id")Long id){
        return shopService.getById(id);
    }
    @GetMapping
    public List<Shop> getAll(){
        return shopService.getAll();
    }
//    添加
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
//        两构造方法
        System.out.println(shop);
        try {
            if (shop.getId()==null){
                shopService.add(shop);
            }else {
                shopService.upadte(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
//    删除
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id")Long id){
//        使用静态方法
        try {
            shopService.delete(id);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

//    高级查询
//    前台是通过Json对象传递进来的，而且是放到RequestBody
    @PostMapping
    public Pagelist<Shop> querys(@RequestBody ShopQuery shopQuery){
        return shopService.query(shopQuery);
    }

    @PatchMapping
    public AjaxResult delPatch(@RequestBody List<Long> ids){
        try {
            shopService.delpatch(ids);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败"+e.getMessage());
        }
    }
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAudit shopAudit){
        try {
            shopService.reject(shopAudit);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败"+e.getMessage());
        }
    }
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAudit shopAudit){
        try {
            shopService.pass(shopAudit);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败"+e.getMessage());
        }
    }


}

